package qimao.com.edu.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import qimao.com.edu.dao.BaseDao;
import qimao.com.edu.dao.CategoryDao;
import qimao.com.edu.dao.ChaptersDao;
import qimao.com.edu.entity.Author;
import qimao.com.edu.entity.Chapters;
import qimao.com.edu.entity.Works;

public class ChaptersDaoImpl implements ChaptersDao {

	@Override
	public int addChapter(Chapters chapters) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		int result = 0;
		String sql = "INSERT INTO chapters (work_id, chapter_id, chapter_title, content_path, word_count, update_time) "
				+ " VALUES (?, ?, ?, ?, ?, NOW())";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
	        ps.setInt(1, chapters.getWorkId());
	        ps.setInt(2, chapters.getChapterId());
	        ps.setString(3, chapters.getChapterTitle());
	        ps.setString(4, chapters.getContentPath());
	        ps.setObject(5, chapters.getWordCount());
	        result = ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, null);
		}
		return result;
	}

	@Override
	public int updateWork(Works works) {
	    Connection con = null;
	    PreparedStatement ps = null;
	    int result = 0;
	    StringBuilder sql = new StringBuilder();
	    List<Object> params = new ArrayList<>();
	    
	    try {
	        // 动态构建SQL语句
	        sql.append("UPDATE works SET ");
	        
	        // 根据传入的works对象设置需要更新的字段
	        boolean hasFields = false;
	        
	        if (works.getTitle() != null) {
	            sql.append("title = ?, ");
	            params.add(works.getTitle());
	            hasFields = true;
	        }
	        Integer chapterCount = works.getChapterCount();
	        if (chapterCount != null) {
	            sql.append("chapter_count = ?, ");
	            params.add(chapterCount);
	            hasFields = true;
	        }
	        
	        if (works.getCover() != null) {
	            sql.append("cover = ?, ");
	            params.add(works.getCover());
	            hasFields = true;
	        }
	        
	        if (works.getDescription() != null) {
	            sql.append("description = ?, ");
	            params.add(works.getDescription());
	            hasFields = true;
	        }
	        
	        if (works.getChapterCount() != null) {
	            sql.append("chapter_count = ?, ");
	            params.add(works.getChapterCount());
	            hasFields = true;
	        }
	        
	        if (works.getWordCount() != null) {
	            sql.append("word_count = ?, ");
	            params.add(works.getWordCount());
	            hasFields = true;
	        }
	        
	        if (works.getPrice() != null) {
	            sql.append("price = ?, ");
	            params.add(works.getPrice());
	            hasFields = true;
	        }
	        
	        if ((Integer)works.getIsOriginal() != null) {
	            sql.append("is_original = ?, ");
	            params.add(works.getIsOriginal());
	            hasFields = true;
	        }
	        
	        if (works.getVipType() != null) {
	            sql.append("vip_type = ?, ");
	            params.add(works.getVipType());
	            hasFields = true;
	        }
	        
	        if (works.getStatus() != null) {
	            sql.append("status = ?, ");
	            params.add(works.getStatus());
	            hasFields = true;
	        }
	        
	     // 更新审核状态
	        Integer status = works.getStatus();
	        if (status != null) {
	            sql.append("status = ?, ");
	            params.add(status);
	            hasFields = true;
	        }
	        
	        // 总是更新最后修改时间
	        sql.append("update_time = NOW() ");
	        
	        // 添加WHERE条件
	        sql.append("WHERE work_id = ?");
	        params.add(works.getWorkId());
	        
	        // 如果没有可更新的字段，直接返回
	        if (!hasFields) {
	            return 0;
	        }
	        
	        con = BaseDao.getCon();
	        ps = con.prepareStatement(sql.toString());
	        
	        // 设置参数
	        for (int i = 0; i < params.size(); i++) {
	            ps.setObject(i + 1, params.get(i));
	        }
	        
	        result = ps.executeUpdate();
	    } catch (SQLException e) {
	        e.printStackTrace();
	    } finally {
	        BaseDao.closeAll(con, ps, null);
	    }
	    
	    return result;
	}

	@Override
	public boolean deleteChapter(int workId, int chapterId) {
	    Connection con = null;
	    PreparedStatement ps = null;
	    boolean result = false;
	    String sql = "DELETE FROM chapters WHERE work_id = ? AND chapter_id = ?";
	    try {
	        con = BaseDao.getCon();
	        ps = con.prepareStatement(sql);
	        // 正确设置参数
	        ps.setInt(1, workId);    // 设置作品ID
	        ps.setInt(2, chapterId); // 设置章节ID
	        int rowsAffected = ps.executeUpdate();
	        result = rowsAffected > 0; // 如果有记录被删除，返回 true
	    } catch (SQLException e) {
	        e.printStackTrace();
	    } finally {
	        BaseDao.closeAll(con, ps, null);
	    }
	    return result;
	}
}

	


